var Scroll = function(obj){
	this.element = $('#scroll');
	this.trigger = obj;
	
	this.cache = {
		//内容最大滚动值
		contMaxScrollTop: 0,
		//滚动条最大滚动值
		scMaxScrollTop: 0,
		rate: null
	}
}
Scroll.prototype = {
	render: function(){
		var self = this;

		self.resetScrollBar();
		self.bindScroll();

		$(window).resize(function(){
			self.resetScrollBar();
		});
	},

	resetScrollBar: function(){
		var cache = this.cache;
		var trigger = this.trigger;
		//内容容器高度
		var wrapHeight = parseInt(trigger.height(), 10) - 5;
		//滚动内容高度
		var scrollHeight = parseInt(trigger.get(0).scrollHeight);
		//滚动条区域高度
		var swHeight = wrapHeight;
		//内容最大滚动值
		var contMaxScrollTop = scrollHeight - wrapHeight;
		//滚动条最大滚动值
		var scMaxScrollTop = 0;
		//滚动条高度
		var height = parseInt(wrapHeight * 500/scrollHeight, 10);
		//内容滚动-滚动条比例
		var rate = 0;

		if(height < 15){height = 15;}

		scMaxScrollTop = swHeight - height;
		rate = (wrapHeight - height)/contMaxScrollTop;

		//重设高度
		this.element.height(height);

		cache.contMaxScrollTop = contMaxScrollTop;
		cache.scMaxScrollTop = scMaxScrollTop;
		cache.rate = rate;
	},

	bindScroll: function(){
		var timer = null;
		var element = this.element;
		var trigger = this.trigger;
		var rate = this.cache.rate;
		var scMaxScrollTop = this.cache.scMaxScrollTop;
		var contMaxScrollTop = this.cache.contMaxScrollTop;

		trigger.scroll(function(){
			var top = parseInt($(this).scrollTop() * rate);

			trigger.parent().addClass('sc-active');
			
			clearTimeout(timer);
			timer = setTimeout(function(){
				trigger.parent().removeClass('sc-active');
			}, 800);

			element.css('top', top);
		});
	}
}